package com.yunkeji.config.log;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.p6spy.engine.spy.appender.MessageFormattingStrategy;
import lombok.extern.slf4j.Slf4j;

/**
 * P6spy SQL 打印策略
 *
 * @author hubin
 * @since 2019-02-20
 */
@Slf4j
public class P6spLog implements MessageFormattingStrategy {

    @Override
    public String formatMessage(
            int connectionId,
            String now,
            long elapsed,
            String category,
            String prepared,
            String sql,
            String url) {
        log.info(
                StringUtils.isNotBlank(sql)
                        ? " 执行时间："
                                + elapsed
                                + " ms "
                                + now
                                + "\n 执行 SQL："
                                + sql.replaceAll("[\\s]+", " ")
                                + "\n"
                        : "");
        return null;
    }
}
